package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;

import network.Buddy;
import network.Status;

public class DataManager implements Iterable<User> {

	private UserCollection users;
	private String dataFileName;

	public DataManager(String dataFile) {


		dataFileName = dataFile;
		users = new UserCollection();
		ObjectInputStream inputStream = null;
		boolean existFile = false;
		try {
			File f = new File(dataFileName);
			existFile = f.exists(); 
			if(!existFile)
				f.createNewFile();

			inputStream = new ObjectInputStream(new FileInputStream(dataFileName));


			Object obj = inputStream.readObject();

			if(obj instanceof UserCollection) {
				users = (UserCollection) obj;
				users.setAllStatus(Status.OFFLINE);
			}

		}catch (ClassCastException ex){
			System.out.println("Ficheiro corrompido!!");
			ex.printStackTrace();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (FileNotFoundException ex) {
			System.out.println("O Ficheiro de dados nao foi encontrado. Novo Ficheiro Criado");
			ex.printStackTrace();
		} catch (IOException ex) {
			if(!existFile)
				System.out.println("O Ficheiro de dados nao foi encontrado. Novo Ficheiro Criado");
		} finally {
			//Close the ObjectInputStream
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}


	public synchronized void add(User user) {
		if(users.containsUser(user.getUserName()))
			users.updateUser(user);
		else
			users.addUser(user);
		writeToFile();
	}

	public synchronized User getUser(String name){
		return users.getUser(name);
	}


	public synchronized boolean isUserOnline(User u){
		return users.isUserOnline(u);
	}


	public synchronized void remove(String userName) {
		users.removeUser(userName);
		writeToFile();
	}

	public synchronized void setAllUsersStatus(Status s){
		users.setAllStatus(s);
		writeToFile();
	}

	public synchronized void makeBackup(){
		writeToFile();
	}

	public synchronized void setUser(User user){
		this.remove(user.getUserName());
		this.add(user);
		writeToFile();
	}

	public synchronized boolean validLogin(User user){
		User u = getUser(user.getUserName());
		return (u.getPassword().equals(user.getPassword()));
	}


	private synchronized void writeToFile(){
		try {
			ObjectOutputStream outStream = new ObjectOutputStream(new FileOutputStream(dataFileName));
			outStream.writeObject(users);
			outStream.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@Override
	public synchronized String toString() {
		return users.toString();
	}


	@Override
	public Iterator<User> iterator() {
		return users.iterator();
	}

	public void updateUsersBuddy(String name, Status st){
		Buddy b = new Buddy (name,st);
		for(User u : users){
			u.updateBuddy(b);
		}
	}

}